<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单编号" prop="orderCoding">
      <el-input v-model="dataForm.orderCoding" placeholder="订单编号"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="createdate">
      <el-input v-model="dataForm.createdate" placeholder="下单时间"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="orderState">
      <el-input v-model="dataForm.orderState" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="支付状态" prop="payState">
      <el-input v-model="dataForm.payState" placeholder="支付状态"></el-input>
    </el-form-item>
    <el-form-item label="发货状态" prop="deliverState">
      <el-input v-model="dataForm.deliverState" placeholder="发货状态"></el-input>
    </el-form-item>
    <el-form-item label="收货状态" prop="takeState">
      <el-input v-model="dataForm.takeState" placeholder="收货状态"></el-input>
    </el-form-item>
    <el-form-item label="支付编码" prop="payCoding">
      <el-input v-model="dataForm.payCoding" placeholder="支付编码"></el-input>
    </el-form-item>
    <el-form-item label="订单总金额" prop="sumPrice">
      <el-input v-model="dataForm.sumPrice" placeholder="订单总金额"></el-input>
    </el-form-item>
    <el-form-item label="订单实付金额" prop="practicalPrice">
      <el-input v-model="dataForm.practicalPrice" placeholder="订单实付金额"></el-input>
    </el-form-item>
    <el-form-item label="退款处理备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="退款处理备注"></el-input>
    </el-form-item>
    <el-form-item label="退款处理状态  1:同意退款   0:拒绝退款" prop="dispose">
      <el-input v-model="dataForm.dispose" placeholder="退款处理状态  1:同意退款   0:拒绝退款"></el-input>
    </el-form-item>
    <el-form-item label="物流ID 外键" prop="logisticsId">
      <el-input v-model="dataForm.logisticsId" placeholder="物流ID 外键"></el-input>
    </el-form-item>
    <el-form-item label="用户ID 外键" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户ID 外键"></el-input>
    </el-form-item>
    <el-form-item label="商品ID 外键" prop="commodityId">
      <el-input v-model="dataForm.commodityId" placeholder="商品ID 外键"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderCoding: '',
          createdate: '',
          orderState: '',
          payState: '',
          deliverState: '',
          takeState: '',
          payCoding: '',
          sumPrice: '',
          practicalPrice: '',
          remark: '',
          dispose: '',
          logisticsId: '',
          userId: '',
          commodityId: ''
        },
        dataRule: {
          orderCoding: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          createdate: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ],
          orderState: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          payState: [
            { required: true, message: '支付状态不能为空', trigger: 'blur' }
          ],
          deliverState: [
            { required: true, message: '发货状态不能为空', trigger: 'blur' }
          ],
          takeState: [
            { required: true, message: '收货状态不能为空', trigger: 'blur' }
          ],
          payCoding: [
            { required: true, message: '支付编码不能为空', trigger: 'blur' }
          ],
          sumPrice: [
            { required: true, message: '订单总金额不能为空', trigger: 'blur' }
          ],
          practicalPrice: [
            { required: true, message: '订单实付金额不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '退款处理备注不能为空', trigger: 'blur' }
          ],
          dispose: [
            { required: true, message: '退款处理状态  1:同意退款   0:拒绝退款不能为空', trigger: 'blur' }
          ],
          logisticsId: [
            { required: true, message: '物流ID 外键不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户ID 外键不能为空', trigger: 'blur' }
          ],
          commodityId: [
            { required: true, message: '商品ID 外键不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/goods/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderCoding = data.order.orderCoding
                this.dataForm.createdate = data.order.createdate
                this.dataForm.orderState = data.order.orderState
                this.dataForm.payState = data.order.payState
                this.dataForm.deliverState = data.order.deliverState
                this.dataForm.takeState = data.order.takeState
                this.dataForm.payCoding = data.order.payCoding
                this.dataForm.sumPrice = data.order.sumPrice
                this.dataForm.practicalPrice = data.order.practicalPrice
                this.dataForm.remark = data.order.remark
                this.dataForm.dispose = data.order.dispose
                this.dataForm.logisticsId = data.order.logisticsId
                this.dataForm.userId = data.order.userId
                this.dataForm.commodityId = data.order.commodityId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/goods/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderCoding': this.dataForm.orderCoding,
                'createdate': this.dataForm.createdate,
                'orderState': this.dataForm.orderState,
                'payState': this.dataForm.payState,
                'deliverState': this.dataForm.deliverState,
                'takeState': this.dataForm.takeState,
                'payCoding': this.dataForm.payCoding,
                'sumPrice': this.dataForm.sumPrice,
                'practicalPrice': this.dataForm.practicalPrice,
                'remark': this.dataForm.remark,
                'dispose': this.dataForm.dispose,
                'logisticsId': this.dataForm.logisticsId,
                'userId': this.dataForm.userId,
                'commodityId': this.dataForm.commodityId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
